package com.higgins.eventtracking.client.health.components;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.visualization.client.DataTable;
import com.google.gwt.visualization.client.VisualizationUtils;
import com.google.gwt.visualization.client.AbstractDataTable.ColumnType;
import com.google.gwt.visualization.client.visualizations.LineChart;
import com.google.gwt.visualization.client.visualizations.LineChart.Options;
import com.higgins.eventtracking.client.EventTrackingService;
import com.higgins.eventtracking.client.EventTrackingServiceAsync;
import com.higgins.eventtracking.client.dto.HealthStatsDTO;
import com.higgins.eventtracking.client.health.PopupQuestionBox;

public class HealthOverviewChart extends VerticalPanel	{
	private Runnable onLoadCallback ;
	private final EventTrackingServiceAsync greetingService = GWT
				.create(EventTrackingService.class);
	//private PopupQuestionBox popupBox = new PopupQuestionBox();

	
	public HealthOverviewChart()	{
		initialise();
	}
	private void initialise()	{
		
	}
	public void runCharts(final List<HealthStatsDTO> results)	{
		onLoadCallback = new Runnable() {
			public void run() {
		        	LineChart line = new LineChart(createTable(results), createOptions());
		        	add(line);
		        	setHeight("400px");
		        	//popupBox.checkForQuestions();
	        }};
		
		VisualizationUtils.loadVisualizationApi(onLoadCallback, LineChart.PACKAGE);
	}
	public void executeChart()	{
		Date start = getDateMinusDays(20);
		Date end = new Date();
		
		greetingService.retrieveHealthStats(start, end,
			new AsyncCallback<List<HealthStatsDTO>>() {
				public void onFailure(Throwable caught) {
					runCharts(new ArrayList<HealthStatsDTO>());
				}
				public void onSuccess(final List<HealthStatsDTO> results) {
					runCharts(results);
				}
			});
	}
	
	  private Options createOptions() {
		    Options options = Options.create();
		    options.setWidth(600);
		    options.setHeight(400);
		    options.setTitle("Health Points Chart");
		    options.setMin(0.0);
		    return options;
		  }

		  private DataTable createTable() {
		    int count = 5;
		    DataTable data = DataTable.create();
		    data.addColumn(ColumnType.DATE, "Date");
		    data.addColumn(ColumnType.NUMBER, "Health Points");
		    data.addRows(count);
		    for (int i = 0; i < count;i++)	{
		        data.setValue(i, 0, getDateMinusDays(5-i));
		        data.setValue(i, 1, i);
		    }
		    return data;
		  }

		  private DataTable createTable(List<HealthStatsDTO> dtos) {
			    DataTable data = DataTable.create();
			    data.addColumn(ColumnType.DATE, "Date");
			    data.addColumn(ColumnType.NUMBER, "Health Points");
			    data.addRows(dtos.size());
			    
			    int count = 0;
			    for (HealthStatsDTO dto:dtos)	{
			        data.setValue(count, 0, dto.getDate());
			        data.setValue(count, 1, dto.getHealthPoints());
			        count++;
			    }
			    
			    return data;
			  }
		  
		  private Date getDateMinusDays(int days)	{
			  Date date = new Date();
			  int day = date.getDate();
			  date.setDate(day-days);
			  return date;
		  }		
}
